/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.enums.AdviceProfessional;
import br.com.foxline.clinicplus.enums.Gender;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.service.UnattendedService;
import br.com.foxline.clinicplus.util.Caracter;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSearchDoctor implements Serializable {

    @EJB
    DoctorService doctorService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    UnattendedService unattendedService;
    
    private Doctor doctorEdit;
    private Doctor doctorShow;
    private Doctor doctorUnattended;
    private Cbo cbo;
    private Country country;
    private Address address;
    private City city;
    private SystemUser systemUser;
    private String passwd;
    private String passwdConfirmation;
    private Doctor searchDoctor;
    private List<Doctor> doctors;
    private Unattended unattended;
    private List<Unattended> unattendeds;
    private boolean mySchedule;
  

    @PostConstruct
    public void init() {
        mySchedule = false;
        searchDoctor = new Doctor();
        doctors = new ArrayList<Doctor>();

       
        unattended = new Unattended();
        unattendeds = new ArrayList<Unattended>();

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String edit = params.get("doctorEdit");
        String show = params.get("doctorShow");
        String unatended = params.get("doctorUnattended");

        if (edit != null) {
            doctorEdit = doctorService.find(Long.parseLong(edit));
            cbo = doctorEdit.getCbo();
            country = doctorEdit.getCountry();
            address = doctorEdit.getAddress();
            city = address.getCity();
            systemUser = doctorEdit.getSystemUser();
        } else if (show != null) {
            doctorShow = doctorService.find(Long.parseLong(show));
        } else if (unatended != null) {
            doctorUnattended = doctorService.find(Long.parseLong(unatended));
            mySchedule = true;
        }
    }

    public void search() {
        doctors = doctorService.findDoctor(searchDoctor);
    }

    public void update() {
        address.setZipcode(Caracter.removecaracter(address.getZipcode()));
        doctorEdit.setCellular(Caracter.removecaracter(doctorEdit.getCellular()));
        doctorEdit.setPhone(Caracter.removecaracter(doctorEdit.getPhone()));
        doctorEdit.setCbo(cbo);
        address.setCity(city);
        doctorEdit.setAddress(address);
        doctorEdit.setCountry(country);

        doctorService.update(doctorEdit);
        Msg.messagemInfo("Atualizado com Sucesso !");

    }

   
    public void updateUnattended() {
        try {
            if (unattended.getStartTime().getTime() > unattended.getEndTime().getTime()) {
                Msg.messagemWarn("A data de início não poderá ser maior que a data final !");
            } else {
                unattended.setCreateAt(new Date());
//                unattendeds = doctorService.findUnattended(doctorUnattended);
                unattended.setDoctor(doctorUnattended);
//                doctorUnattended.setUnattendeds(unattendeds);
                if(!mySchedule){
                    unattended.setDescription(unattended.getDescription()+", Criado por: "+systemUserService.getCurrentUser().getName());
                }
                unattendedService.save(unattended);
                unattended = new Unattended();
                Msg.messagemInfo("Cadastrado com Sucesso !");
            }
        } catch (Exception erro) {
            erro.printStackTrace();
        }
    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> selectAdviceProfessional() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (AdviceProfessional ms : AdviceProfessional.values()) {
            genders.add(new SelectItem(ms, ms + ""));
        }
        return genders;
    }

    public boolean isMySchedule() {
        return mySchedule;
    }

    public void setMySchedule(boolean mySchedule) {
        this.mySchedule = mySchedule;
    }

    
    public Doctor getDoctorEdit() {
        return doctorEdit;
    }

    public void setDoctorEdit(Doctor doctorEdit) {
        this.doctorEdit = doctorEdit;
    }

    public List<Doctor> autocomplete(String query) {
        return doctorService.findAll();
    }

    public Cbo getCbo() {
        return cbo;
    }

    public void setCbo(Cbo cbo) {
        this.cbo = cbo;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getPasswdConfirmation() {
        return passwdConfirmation;
    }

    public void setPasswdConfirmation(String passwdConfirmation) {
        this.passwdConfirmation = passwdConfirmation;
    }

    public List<Doctor> getDoctors() {
        return doctors;
    }

    public void setDoctors(List<Doctor> doctors) {
        this.doctors = doctors;
    }

    public Doctor getDoctorShow() {
        return doctorShow;
    }

    public void setDoctorShow(Doctor doctorShow) {
        this.doctorShow = doctorShow;
    }

    public Doctor getSearchDoctor() {
        return searchDoctor;
    }

    public void setSearchDoctor(Doctor searchDoctor) {
        this.searchDoctor = searchDoctor;
    }

    public Unattended getUnattended() {
        return unattended;
    }

    public void setUnattended(Unattended unattended) {
        this.unattended = unattended;
    }

    public List<Unattended> getUnattendeds() {
        return unattendeds = unattendedService.findUnattended(doctorUnattended);
    }

    public void setUnattendeds(List<Unattended> unattendeds) {
        this.unattendeds = unattendeds;
    }

    public Doctor getDoctorUnattended() {
        return doctorUnattended;
    }

    public void setDoctorUnattended(Doctor doctorUnattended) {
        this.doctorUnattended = doctorUnattended;
    }

    public void removerUnattended(int id){
        Unattended rem = unattendeds.get(id);
        unattendeds.remove(id);
        unattendedService.delete(rem);
        Msg.messagemInfo("Excluido com Sucesso!");
    }
}
